package ca.kooki.codesnippet.core;

import java.text.ParseException;
import java.util.Date;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class ActionTest {

  
  @Before
  @After
  public void setUpAndTearDown()
  {
    SystemDate.reset();
  }


  @Test
  public void Constructors() throws ParseException
  {
    SystemDate.setNowTo("Nov 4, 2003 8:14 PM");
    
    Action action = new Action("add","adding a new user");
    Assert.assertEquals("add",action.getType());
    Assert.assertEquals("adding a new user",action.getDescription());
    Assert.assertEquals(true,action.getSuccess());
    Assert.assertEquals(SystemDate.Now(),action.when());
    Assert.assertEquals(User.ANNONYMOUS, action.getUser());
  }
  
  @Test
  public void By()
  {
    Action action = new Action("1","2");
    Assert.assertEquals(User.ANNONYMOUS,action.getUser());
    
    User user = new User("aforward");
    
    action.setUser(user);
    Assert.assertEquals(user,action.getUser());
  }
  
  @Test
  public void ToString() throws ParseException
  {
    SystemDate.setNowTo("Nov 4, 2003 8:14 PM");
    
    Action action = new Action("add","adding a new user");
    Assert.assertEquals("add, adding a new user, Nov 4, 2003 8:14 PM",action.toString());
    
    action.setSuccess(false);
    Assert.assertEquals("[failed] add, adding a new user, Nov 4, 2003 8:14 PM",action.toString());
    
  }
  
  @Test
  public void setDate() throws ParseException
  {
    Action action = new Action("a","b");
    
    Date when = SystemDate.parse("Nov 4, 2003 8:14 PM");
    
    action.setDate(when);
    Assert.assertEquals(when, action.when());
  }
  
  @Test
  public void NotEqualsWrongType()
  {
    Action action = new Action("aha","aha2");
    Assert.assertEquals(false, action.equals(null));
    Assert.assertEquals(false, action.equals("wrong type"));
  }

  @Test
  public void NotEqualsWrongInputs() throws ParseException
  {
    SystemDate.setNowTo("Nov 4, 2003 8:14 PM");

    Action action = new Action("1","2");
    Action compareTo = new Action("1","3");
    Assert.assertEquals(false, action.equals(compareTo));

    compareTo = new Action("3","2");
    Assert.assertEquals(false, action.equals(compareTo));

    compareTo = new Action("1","2");
    compareTo.setSuccess(false);
    Assert.assertEquals(false, action.equals(compareTo));

    SystemDate.setNowTo("Nov 5, 2003 8:14 PM");
    compareTo = new Action("1","2");
    Assert.assertEquals(false, action.equals(compareTo));
    
  }

  @Test
  public void EqualsSimilarTime() throws ParseException, InterruptedException
  {
    Action action = new Action("1","2");
    Thread.sleep(100);
    
    Action compareTo = new Action("1","2");

    Assert.assertEquals(true, action.equals(compareTo));
  }

  
  @Test
  public void EqualsSameInfo()
  {
    Action action = new Action("1","2");
    Action compareTo = new Action("1","2");
    Assert.assertEquals(true, action.equals(compareTo));
  }
  
  @Test
  public void GetSuccessText()
  {
    Action action = new Action("1","2");
    action.setSuccess(true);
    Assert.assertEquals("true",action.getSuccessText());
    action.setSuccess(false);
    Assert.assertEquals("false", action.getSuccessText());
  }
  
}
